/**
* Desc: 路由
* User: cuixiaonai
* Email: 15965873315@163.com
* Date: 2022/1/14 1:06 PM
 */
package route

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go_gin_mvc/src/app/controller/Index"
	"go_gin_mvc/src/app/middleware"
	config2 "go_gin_mvc/src/config"
	"go_gin_mvc/src/pkg/logging"
)

func Init() *gin.Engine {
	// 获取配置文件
	config := config2.Config()
	router := gin.Default()
	// 信任所有代理
	// router.SetTrustedProxies([]string{"192.168.1.2"})
	// If the client is 192.168.1.2, use the X-Forwarded-For
	// header to deduce the original client IP from the trust-
	// worthy parts of that header.
	// Otherwise, simply return the direct client IP
	err := router.SetTrustedProxies(nil)
	if err != nil {
		logging.Error("初始化信任代理出错", err)
		panic(err)
	}
	// 注册模板
	router.LoadHTMLGlob("templates/views/**/*")
	// 注册静态文件:参数1：别名、参数2：当前static文件目录，
	router.Static("static", "./templates/static")
	// 跨域使用
	router.Use(middleware.Cors())
	// 路由配置
	router.GET("/", Index.IndexApi)
	router.GET("/indexhtml", Index.IndexHtml)

	// 开始服务
	err = router.Run(fmt.Sprintf("%s:%s", config.Self.Host, config.Self.Port))
	if err != nil {
		logging.Error("初始化路由出错", err)
		panic(err)
	}
	return router
}
